<?php

namespace App\Model;

/**
 * Class User
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/31 0031
 * @Time 15:25
 */
class User extends Common
{
    /**
     * @var string
     */
    protected $primaryKey = 'user_id';


    /**
     * 方法 : add_user
     * @User wuqin
     * @DateTime 2018-03-31 15:24
     * @param $var
     * @return User|bool
     */
    public function add_user($var)
    {
        $this->openid = $var['openid'];
        $this->nickname = $var['nickname'];
        $this->sex = $var['sex'];
        $this->country = $var['country'];
        $this->province = $var['province'];
        $this->city = $var['city'];
        $this->balance = 0.00;
        $this->headimgurl = $var['headimgurl'];
        return $this->save() ? $this : false;
    }


    /**
     * 方法 : update_user
     * @User wuqin
     * @DateTime 2018-03-31 15:24
     * @param $var
     * @return User|bool
     */
    public function update_user($var)
    {

        $this->nickname = $var['nickname'];
        $this->sex = $var['sex'];
        $this->country = $var['country'];
        $this->province = $var['province'];
        $this->city = $var['city'];
        $this->headimgurl = $var['headimgurl'];

        return $this->save() ? $this : false;
    }

    /**
     * 方法 : get_user_by_open_id
     * @User wuqin
     * @DateTime 2018-03-31 15:25
     * @param $open_id
     * @return bool
     */
    public function get_user_by_open_id($open_id)
    {
        if (empty($open_id))
            return false;

        $where = array(
            array("where", "openid", "=", $open_id)
        );

        $res = $this->get($where, "*", 0, 0);
        if (empty($res) || count($res) !== 1)
            return false;
        else
            return $res[0];
    }

    /**
     * 方法 : get_user_by_open_id
     * @User wuqin
     * @DateTime 2018-03-31 15:25
     * @param $open_id
     * @return bool
     */
    public function get_user_by_user_id($user_id)
    {
        if (empty($user_id))
            return false;

        $where = array(
            array("where", "openid", "=", $user_id)
        );

        $res = $this->get($where, "*", 0, 0);
        if (empty($res) || count($res) !== 1)
            return false;
        else
            return $res[0];
    }
}
